from django.shortcuts import render
from django.http import HttpResponse
from .qksw import Qksw
import json,requests

# Create your views here.


def search(request):
    
    info = dict(request.POST)
    keyword = info['keyword'][0]
    print(keyword)
    a = Qksw().search(keyword)
    
    return HttpResponse(json.dumps(a))


def homepage(request):
    info = dict(request.POST)
    homepage = info['homepage']
    a = Qksw().homepage()

    return HttpResponse(json.dumps(a))
    
    

def chapter_info(request):
    info = dict(request.POST)
    novel_url = info['novel_url'][0]
    novel_1 = novel_url.split('/')[-3]
    novel_2 = novel_url.split('/')[-2]
    
    url = 'https://www.qksw.com/{}/{}/'.format(novel_1,novel_2)
    info = Qksw().all_book_info(url)

    return HttpResponse(json.dumps(info))


def chapter_content(request):

    info = dict(request.POST)
    chapter_url = info['chapter_url'][0]
    chapter_1 = chapter_url.split('/')[-3]
    chapter_2 = chapter_url.split('/')[-2]
    chapter_3 = chapter_url.split('/')[-1]
    
    url = 'https://www.qksw.com/{}/{}/{}'.format(chapter_1,chapter_2,chapter_3)
    info = Qksw().chapter_content(url)

    return HttpResponse(json.dumps(info))



    
